package com.ChenYaRou.www.dao.impl;

import com.ChenYaRou.www.dao.FoodDao;
import com.ChenYaRou.www.po.Food;
import com.ChenYaRou.www.utils.GenerateSql;
import com.ChenYaRou.www.utils.JdbcUtil;

import java.sql.SQLException;
import java.util.List;

/**
 * @ClassName FoodDapImpl
 * @Description TODO
 * @Author 86183
 * @Date 2022/4/11 23:45
 * @Version 1.0
 **/
public class FoodDapImpl implements FoodDao {

    /**
     * 新建美食
     * @param food 新美食
     * @return 更新条数
     * @throws IllegalAccessException
     * @throws SQLException
     */
    @Override
    public int insert(Food food) throws IllegalAccessException, SQLException {
        return JdbcUtil.getInstance().insertOrUpdate(GenerateSql.getInstance().insert(food));
    }

    /**
     * 更新food
     * @param newFood 新赋值内容
     * @param oldFood 更新的food
     * @return 更新条数
     * @throws IllegalAccessException
     * @throws SQLException
     */
    @Override
    public int update(Food newFood,Food oldFood) throws IllegalAccessException, SQLException {
        return JdbcUtil.getInstance().insertOrUpdate(GenerateSql.getInstance().update(newFood,oldFood));
    }

    /**
     * 查找food集合
     * @param food 条件food
     * @return
     * @throws IllegalAccessException
     * @throws SQLException
     * @throws NoSuchFieldException
     * @throws InstantiationException
     */
    @Override
    public List<Food> selectMore(Food food) throws IllegalAccessException,
            SQLException, NoSuchFieldException, InstantiationException {
        return JdbcUtil.getInstance().selectMore(GenerateSql.getInstance().select(food), Food.class);
    }

    /**
     * 模糊搜索foodName
     * @param food 含有模糊关键字的name的food
     * @return food集合
     * @throws IllegalAccessException
     * @throws SQLException
     * @throws NoSuchFieldException
     * @throws InstantiationException
     */
    @Override
    public List<Food> fuzzySelect(Food food) throws IllegalAccessException, SQLException, NoSuchFieldException, InstantiationException {
        return JdbcUtil.getInstance().selectMore(GenerateSql.getInstance().fuzzySelect(food),Food.class );
    }

    @Override
    public void delete() {

    }

    /**
     * 查找所有food集合
     * @param food 条件food
     * @return
     * @throws IllegalAccessException
     * @throws SQLException
     * @throws NoSuchFieldException
     * @throws InstantiationException
     */
    @Override
    public List<Food> selectAll(Food food) throws IllegalAccessException, SQLException,
            NoSuchFieldException, InstantiationException{
        return JdbcUtil.getInstance().selectMore(GenerateSql.getInstance().selectAll(food), Food.class);
    }
}
